package activities;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import com.google.android.gms.maps.model.LatLng;
import com.searchparking.R;
import com.searchparking.R.layout;
import com.searchparking.R.menu;
import com.searchparking.BUS.DirectionListAdapter;
import com.searchparking.BUS.DirectionsJSONParser;
import com.searchparking.BUS.ListviewAdapter;
import com.searchparking.BUS.ParkingInfo;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class DirectionList extends Activity {
	private TextView address_currenrt;
	private TextView address_destion, kc;
	String address;
	LatLng orgin, dest;
	private Double current_latitude, current_longitude, disten_latitude,
			disten_longitude, disten_latitude_map, disten_longitude_map;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_direction_list);
		Log.d("DIRECTTION", "OK");
		Intent i = getIntent();
		address = i.getStringExtra("Address");

		// get value from search view
		Bundle bundle = getIntent().getExtras();

		current_latitude = MainActivity.latitude;
		current_longitude = MainActivity.longitude;

		disten_latitude_map = bundle.getDouble("DesLatitude_map");
		disten_longitude_map = bundle.getDouble("DesLongitude_map");

		disten_latitude = bundle.getDouble("DeLatitude");
		disten_longitude = bundle.getDouble("DeLongtati");

		// get DestinationAddress
		address = bundle.getString("DestinationAddress");

		address_currenrt = (TextView) findViewById(R.id.tv_current);
		address_destion = (TextView) findViewById(R.id.tv_destion);
		kc = (TextView) findViewById(R.id.txtKhoangcach);
		address_currenrt.setText(MainActivity.address);
		address_destion.setText(address);
		String dutationVal = "";
		String distance = "";
		// 2 diem ao
		// LatLng orgin = new LatLng(16.063960, 108.182518);
		// LatLng dest = new LatLng(16.039379, 108.168442);
		if (disten_longitude == 0) {
			orgin = new LatLng(current_latitude, current_longitude);
			dest = new LatLng(disten_latitude_map, disten_longitude_map);
		} else {
			orgin = new LatLng(current_latitude, current_longitude);
			dest = new LatLng(disten_latitude, disten_longitude);
		}

		Toast.makeText(
				getApplicationContext(),
				current_latitude + " " + current_longitude + "  " + " "
						+ disten_latitude_map + " " + disten_longitude_map + "  " +disten_longitude + "  " + disten_latitude  ,
				Toast.LENGTH_SHORT).show();
		ParkingInfo parking = new ParkingInfo();
		try {
			parking.getDirectionsUrl(orgin, dest);
			String data = parking.getDataUrl();
			distance = parking.getDistance(data);
			dutationVal = parking.getDuaration();
			kc.setText("Khoảng cách:" + distance + " (Khoảng " + dutationVal
					+ ")");
			JSONObject jsonObject = null;
			try {
				jsonObject = new JSONObject(data);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			DirectionsJSONParser direct = new DirectionsJSONParser();
			direct.parse(jsonObject);
			List<HashMap<String, String>> aListDirection = direct
					.getListInstruction();
			DirectionListAdapter listview = new DirectionListAdapter(this,
					aListDirection);
			ListView list = (ListView) findViewById(R.id.listView1);
			list.setAdapter(listview);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.direction_list, menu);
		return true;
	}

}
